<?php

namespace App\Console\Commands;

use App\Service\Redis\RedisBaseService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ReUpdateUserLearnQuestionCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:updateLearnQuestionCount';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新用户试题数量';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws \RedisException
     */
    public function handle()
    {
        $users = DB::table('user')
            ->whereBetween('learnQuestionOfLastTime', [Carbon::createFromTimestamp(1717171200), now()])
            ->where('status', 'normal')
            ->get()->toArray();

        $redisBaseService = new RedisBaseService();

        $successCount = 0;
        foreach ($users as $user) {
            $logs = DB::table('qh_user_question_logs')->where('userId', $user->id)->get();

            $allCount = count($logs);
            $wrongCount = 0;
            $correctCount = 0;
            $todayCount = 0;
            $todayWrongCount = 0;
            $todayCorrectCount = 0;

            DB::beginTransaction();
            try {
                foreach ($logs as $log) {
                    if ($log->result == 1) {
                        $correctCount += 1;
                        if ($log->created_at && Carbon::make($log->created_at)->isToday()) {
                            $todayCount += 1;
                            $todayCorrectCount += 1;
                        }
                    } else  {
                        $wrongCount += 1;
                        if ($log->created_at && Carbon::make($log->created_at)->isToday()) {
                            $todayCount += 1;
                            $todayWrongCount += 1;
                        }
                    }
                }

                // 更新到用户
                DB::table('user')->where('id', $user->id)->update([
                    'learnQuestionCount' => $allCount,
                    'correctQuestionCount' => $correctCount,
                    'wrongQuestionCount' => $wrongCount,
                    'learnQuestionCountOfToday' => $todayCount,
                    'correctQuestionCountOfToday' => $todayCorrectCount,
                    'wrongQuestionCountOfToday' => $todayWrongCount
                ]);

                // 删掉对应用户的 redis缓存
                $redisBaseService->delUserStatisticsData('statistics:userLearnQuestionCount', $user->id);
                $redisBaseService->delUserStatisticsData('statistics:userLearnQuestionOfTodayCount', $user->id);
                $redisBaseService->delUserStatisticsData('statistics:userWrongQuestionCount', $user->id);
                $redisBaseService->delUserStatisticsData('statistics:userCorrectQuestionCount', $user->id);
                $redisBaseService->delUserStatisticsData('statistics:userCorrectQuestionCountOfToday', $user->id);
                $redisBaseService->delUserStatisticsData('statistics:userWrongQuestionCountOfToday', $user->id);
                DB::commit();
                $successCount += 1;
            }catch (\Exception $e) {
                DB::rollBack();
                \Log::error('user:updateLearnQuestionCount');
                \Log::error($e);
                continue;
            }
        }

        dump('总用户数：'.count($users).' | 已处理用户数：'.$successCount);

        return count($users);
    }
}
